package ui;

import org.eclipse.jface.window.Window;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.layout.GridLayout;
import org.eclipse.swt.widgets.Combo;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Control;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

import controls.TitansComposite;
import controls.TitansLabel;
import controls.TitansTitleAreaDialog;

public class SchedulerDialog extends TitansTitleAreaDialog {

	private Combo cmbScheduler;
	private Text txtQuantum;

	private String scheduler;
	private int quantum;

	public SchedulerDialog(Shell shell) {
		super(shell, new Point(300, 300), "Scheduler Options",
				"Choose a scheduler", null);
	}

	protected Control createDialogArea(Composite parent) {
		parent = (Composite) super.createDialogArea(parent);

		parent.setLayout(new GridLayout(1, false));
		parent.setLayoutData(new GridData(GridData.FILL_BOTH));

		TitansComposite cmpContainer = new TitansComposite(parent);
		cmpContainer.setLayoutData(new GridData(GridData.FILL_BOTH));
		cmpContainer.setLayout(new GridLayout(2, false));

		TitansLabel lblScheduler = new TitansLabel(cmpContainer, "Scheduler");
		lblScheduler.setLayoutData(new GridData());

		cmbScheduler = new Combo(cmpContainer, SWT.BORDER | SWT.DROP_DOWN
				| SWT.READ_ONLY);
		cmbScheduler.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));
		cmbScheduler.add("Round Robin");
		cmbScheduler.add("FCFS");
		cmbScheduler.add("SPF");
		cmbScheduler.select(0);

		cmbScheduler.addSelectionListener(new SelectionAdapter() {
			public void widgetSelected(SelectionEvent e) {
				if (cmbScheduler.getSelectionIndex() == 0) {
					txtQuantum.setEnabled(true);
				} else {
					txtQuantum.setEnabled(false);
				}
			}
		});

		TitansLabel lblQuantum = new TitansLabel(cmpContainer, "Quantum");
		lblQuantum.setLayoutData(new GridData());

		txtQuantum = new Text(cmpContainer, SWT.BORDER);
		txtQuantum.setLayoutData(new GridData(GridData.FILL_HORIZONTAL));

		return parent;
	}

	protected void okPressed() {
		scheduler = cmbScheduler.getText();

		if (cmbScheduler.getSelectionIndex() == 0
				&& "".equals(txtQuantum.getText())) {
			this.setErrorMessage("Missing Data!");
			return;
		}

		if (cmbScheduler.getSelectionIndex() == 0) {
			try {
				quantum = Integer.parseInt(txtQuantum.getText());
			} catch (Exception e) {
				this.setErrorMessage("Invalid Input");
				return;
			}
		}
		super.close();
	}

	protected void createButtonsForButtonBar(Composite composite) {
		super.createButtonsForButtonBar(composite);
		this.createButton(composite, Window.OK, "OK", true);
		this.createButton(composite, Window.CANCEL, "Cancel", false);
	}

	public String getScheduler() {
		return scheduler;
	}

	public int getQuantum() {
		return quantum;
	}
}